package com.dayu.utils;

import java.text.SimpleDateFormat;
import java.util.*;

public class RandomOrderUtils {
    /**
     * 批量生成
     * @param num
     * @return
     */
    public static Set<String> randomOrderNumSet(int num) {
        Set<String> numSet = new HashSet<>();
        int falseNum = 0;
        for(int i = 0; i < num; i++) {
            String order = randomOrderNum();
            boolean bool = numSet.add(order);
            if(!bool) {
                falseNum++;
            }
        }
        if(falseNum != 0) {
            randomSetByOne(numSet,falseNum);
        }
        return numSet;
    }

    /**
     * 同一时间数据去重
     * @param set
     * @param num
     */
    public static void randomSetByOne(Set<String> set, int num) {
        int falseNum = 0;
        for(int i = 0; i <num; i++) {
            boolean bool = set.add(randomOrderNum());
            if(!bool) {
                falseNum++;
            }
        }
        if(num != 0) {
            randomSetByOne(set, falseNum);
        }
    }

    /**
     * 日期前缀
     * @return
     */
    public static String randomOrderNum() {
        String order = null;

        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
        order = sdf.format(new Date());
        order += randomNum();
        return order;
    }

    /**
     * 随机尾数
     * @return
     */
    public static String randomNum() {
        Integer num = null;
        num = (int) ((Math.random() * 9 + 1) * 100000);
        return num + "";
    }
}
